<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>依赖注入</h2>
      <div>{{ count }}</div>
      <button @click="count++">btn</button>
      <!-- <child :count="count"></child> -->
      <child></child>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      // const { computed } = Vue;

      const Child2 = {
        data() {
          return {
            count: 999,
          };
        },
        // props: ["count"],
        template: `
          <div>这是child2子组件 - {{ msg }} - {{ newCount }}</div>
        `,
        // inject表示注入，可以接收来自于祖先组件的provide的值
        // 语法和props一样
        // inject: ["msg", "count"],
        // 可以用对象的方式取别名和设置默认值
        inject: {
          msg: "msg",
          newCount: {
            from: "count",
            default: 888,
          },
        },
      };

      const Child = {
        // props: ["count"],
        template: `
          <div>这是child子组件</div>
          <child2></child2>
        `,
        components: {
          Child2,
        },
      };

      Vue.createApp({
        data() {
          return {
            count: 100,
          };
        },
        // provide表示依赖，可以给子孙组件提供数据
        // 语法和data是一样的
        provide() {
          return {
            msg: "hello",
            // count: this.count,
            count: Vue.computed(() => this.count),
          };
        },
        components: {
          Child,
        },
      }).mount("#app");
    </script>
  </body>
</html>
